package com.gamesense.client.module.modules.exploits;

import com.gamesense.api.event.events.PacketEvent;
import com.gamesense.api.settings.Setting;
import com.gamesense.client.GameSenseMod;
import com.gamesense.client.module.Module;
import me.zero.alpine.listener.EventHandler;
import me.zero.alpine.listener.Listener;
import net.minecraft.block.BlockContainer;
import net.minecraft.network.play.client.CPacketEntityAction;
import net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock;
import net.minecraft.util.math.BlockPos;

public class NoInteract extends Module{
	public NoInteract(){
		super("NoInteract", Category.Exploits);
	}

	private Setting.Boolean containerOnly;
	private boolean shouldStop = false;

	public void setup(){
		containerOnly = registerBoolean("Container Only", "ContainerOnly", false);
	}

	@EventHandler
	private final Listener<PacketEvent.Send> sendListener = new Listener<>(event -> {
		if (event.getPacket() instanceof CPacketPlayerTryUseItemOnBlock && !mc.player.isSneaking()){

			CPacketPlayerTryUseItemOnBlock packet = ((CPacketPlayerTryUseItemOnBlock) event.getPacket());
			BlockPos pos = packet.getPos();

			if (containerOnly.getValue() && !(mc.world.getBlockState(pos).getBlock() instanceof BlockContainer)) return;

			mc.player.connection.sendPacket(new CPacketEntityAction(mc.player, CPacketEntityAction.Action.START_SNEAKING));
			shouldStop = true;
		}
	});

	@EventHandler
	private final Listener<PacketEvent.PostSend> postSendListener = new Listener<>(event -> {
		if (event.getPacket() instanceof CPacketPlayerTryUseItemOnBlock && shouldStop){

			CPacketPlayerTryUseItemOnBlock packet = ((CPacketPlayerTryUseItemOnBlock) event.getPacket());
			BlockPos pos = packet.getPos();

			if (containerOnly.getValue() && !(mc.world.getBlockState(pos).getBlock() instanceof BlockContainer)) return;

			mc.player.connection.sendPacket(new CPacketEntityAction(mc.player, CPacketEntityAction.Action.STOP_SNEAKING));
			shouldStop = false;
		}
	});

	public void onEnable(){
		GameSenseMod.EVENT_BUS.subscribe(this);
	}

	public void onDisable(){
		GameSenseMod.EVENT_BUS.unsubscribe(this);
	}

}
